# RUN: llc %s --start-after=livedebugvalues -filetype=obj -o - \
# RUN:     | llvm-dwarfdump - -name local* -regex \
# RUN:     | FileCheck %s
#
# Test that the -trim-var-locs option (enabled by default) works correctly.
# Test directives and comments inline.

--- |
  target triple = "x86_64-unknown-linux-gnu"
  define dso_local i32 @fun() local_unnamed_addr !dbg !7 {
  entry:
    ret i32 0
  }

  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!3, !4, !5}
  !llvm.ident = !{!6}

  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
  !1 = !DIFile(filename: "example.c", directory: "/")
  !2 = !{}
  !3 = !{i32 7, !"Dwarf Version", i32 4}
  !4 = !{i32 2, !"Debug Info Version", i32 3}
  !5 = !{i32 1, !"wchar_size", i32 4}
  !6 = !{!"clang version 11.0.0"}
  !8 = !DISubroutineType(types: !9)
  !9 = !{!10}
  !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  !11 = !{!12, !13, !25}
  !22 = !DISubroutineType(types: !23)
  !23 = !{!10, !10}
  ; --- Important metadata ---
  !7 = distinct !DISubprogram(name: "fun", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
  !24 = distinct !DILexicalBlock(scope: !7, file: !1, line: 9, column: 3)
  !14 = distinct !DILexicalBlock(scope: !7, file: !1, line: 4, column: 3)
  !12 = !DILocalVariable(name: "locala", scope: !7, file: !1, line: 1, type: !10)
  !13 = !DILocalVariable(name: "localb", scope: !14, file: !1, line: 2, type: !10)
  !25 = !DILocalVariable(name: "localc", scope: !24, file: !1, line: 3, type: !10)
  !15 = !DILocation(line: 1, column: 0, scope: !7)
  !18 = !DILocation(line: 2, column: 1, scope: !14)
  !26 = !DILocation(line: 3, column: 1, scope: !24)
...
---
name:            fun
body:             |
  bb.0.entry:
    ; This is the scope and variable structure:
    ; int fun() {       // scope fun !7
    ;   int locala;     // scope fun !7,        var locala !12, debug-location !15
    ;   { int localb; } // scope fun:block !14, var localb !13, debug-location !18
    ;   { int localc; } // scope fun:block !24, var localc !25, debug-location !26
    ; }
    ;
    ; (1) Check that a variable location range found in implied scope fun !7 is
    ;     not trimmed.
    ;
    ; CHECK:      DW_TAG_variable
    ; CHECK-NEXT:   DW_AT_location
    ; CHECK-NEXT:     DW_OP_reg0 RAX
    ; CHECK-NEXT:   DW_AT_name ("locala")
    ;
    ; scope fun !7 is implied as we're in function fun and haven't seen a debug-location
    $eax = MOV32ri 0
    ; locala range 1 start in implicit scope fun !7
    DBG_VALUE $eax, $noreg, !12, !DIExpression(), debug-location !15
    $edi = MOV32ri 1
    ; locala range 1 clobber in implicit scope fun !7
    $eax = MOV32ri 2
    ; scope fun !7 explicit start
    $edi = MOV32ri 3, debug-location !15

    ; (2) Check that a variable location range found outside lexical block is
    ;     trimmed. See check directives for (3).
    ;
    ; localb range 1 start in scope fun !7 (outside block !14).
    DBG_VALUE $eax, $noreg, !13, !DIExpression(), debug-location !18
    ; localb range 1 clobber in scope fun !7
    $edi = MOV32ri 4, debug-location !15

    ; (3) Check that a variable location range which overlaps the entire lexical
    ;     block is not trimmed.
    ;
    ; CHECK:      DW_TAG_variable
    ; CHECK-NEXT:   DW_AT_location (DW_OP_reg5 RDI)
    ; CHECK-NEXT:   DW_AT_name ("localb")
    ;
    ; localb range 2 clobber in scope fun !7 (outside block !14)
    DBG_VALUE $edi, $noreg, !13, !DIExpression(), debug-location !18
    ; scope block !14 start (and only instruction)
    $edi = MOV32ri 5, debug-location !18

    ; (4) Check that a variable location range in scope fun !7 (outside block
    ;     !14) is trimmed. See check directives for (3).
    ;
    ; localb range 3 starts after scope !14 (prev instr is last in scope)
    DBG_VALUE $rax, $noreg, !13, !DIExpression(), debug-location !18
    ; scope block !14 end
    $edi = MOV32ri 6, debug-location !15

    ; (5) Check that a variable location range found between disjoint scope
    ;     ranges is trimmed.
    ;
    ; CHECK:      DW_TAG_variable
    ; CHECK-NOT:    DW_AT_location
    ; CHECK-NEXT:   DW_AT_name ("localc")
    ;
    ; scope fun !7
    $edi = MOV32ri 6, debug-location !15
    ; scope block !24 start and end range 1
    $edi = MOV32ri 7, debug-location !26
    ; localc range 1 start in scope !7
    DBG_VALUE $edi, $noreg, !25, !DIExpression(), debug-location !18
    ; localc range 1 clobber in scope !7
    $edi = MOV32ri 8, debug-location !15
    ; scope block !24 start and end range 2
    $edi = MOV32ri 9, debug-location !26

    ; scope fun !7
    RET64 debug-location !15
...
